/**
 * Created by Jie on 2017/2/4.
 */
var tool = require('./tools');
module.exports = {
    one:function (_table,_id) {
        return "select * from "+_table+" where id="+_id;
    },
    check:function (_table,_where) {
        return "select 1 from "+_table+" where isDel=0 "+_where;
    },
    insert:function (_table,_obj) {
        var sql = 'insert into '+ _table + ' (';
        var attrs = [];
        var vals = [];
        for(var key in _obj){
            attrs.push('`'+key+'`');
            vals.push("'"+_obj[key]+"'");
        }
        return sql+attrs.join(',')+") values("+vals.join(',')+");";
    },
    inserts:function (_table,_list) {
        var sql = 'insert into '+ _table + ' (';
        var attrs = [];
        var data = '';
        for(var k=0;k<_list.length;k++){
            var vals = [];
            var _obj = _list[k];
            for(var key in _obj){
                if(k==0){
                    attrs.push('`'+key+'`');
                }
                vals.push("'"+_obj[key]+"'");
            }
            data +='('+vals.join(',')+'),'
        }
        data = data.substring(0,data.length-1);
        return sql+attrs.join(',')+") values"+data+";";
    },
    update:function (_table,_obj) {
        var id = 0;
        var vals = [];
        for(var key in _obj){
            if(key == 'id'){
                id = _obj[key];
            }else{
                vals.push('`'+key+"`='"+_obj[key]+"'");
            }
        }
        return "update "+_table+" set "+vals.join(',')+' where id='+id;
    },
    delete:function (_table,_params) {
        return "update "+_table+" set isDel=1,updateTime='"+tool.time()+"',updateId='"+_params.updateId+"' where id="+_params.id;
    },
    opt:function (_table,_where) {
        return "update "+_table+" set isDel=1 where isDel=0 "+_where;
    },
    select:function (_table,_where,_ids) {
        var field = (typeof _where.field) == 'undefined'?' * ':_where.field;
        var orderBy = (typeof _where.order) == 'undefined'?' a.id desc ':_where.order;
        var join = (typeof _where.join) == 'undefined'?' ':_where.join;

        return "select "+field+" from "+_table+" a "+ join+ " where a.id in ("+_ids+") order by "+orderBy;
    },
    count:function (_table,_where) {
        var orderBy = (typeof _where.order) == 'undefined'?' a.id desc ':_where.order;
        var join = (typeof _where.join) == 'undefined'?' ':_where.join;
        var where = (typeof _where.where) == 'undefined'?' ':_where.where;
        return "select count(1) rows from " + _table + " a "+ join +" where a.isDel=0 " + where + " order by " + orderBy;
    },
    ids:function (_table,_where) {
        var index = _where.index;
        var size = _where.size;
        size = size<0?10:size; //防止分页攻击
        var start = (index-1) * size;

        var orderBy = (typeof _where.order) == 'undefined'?' a.id desc ':_where.order;
        var join = (typeof _where.join) == 'undefined'?' ':_where.join;
        var where = (typeof _where.where) == 'undefined'?' ':_where.where;


       return "select a.id from " + _table + " a "+ join +" where a.isDel=0 " + where + "order by " + orderBy +" limit "+start+","+size;
    },
    where:function (_obj) {
        
    },
    all:function (_table,_where,_order) {
        return "select * from "+_table+" where isDel=0 "+_where +" order by "+ _order;
    },
    find:function (_table,_where) {
        return "select * from "+_table+" where isDel=0 "+_where;
    },
    findIn:function (_table,_ids) {
        return "select * from "+_table+" where isDel=0 id in("+_ids+")";
    }
};